TOOL.Category = '(Malawar)'
TOOL.Name = '#Airbrake'
TOOL.Command = nil
TOOL.ConfigName = ''

TOOL.ClientConVar['key'] = '5'
TOOL.ClientConVar['passive'] = '10.0'
TOOL.ClientConVar['active'] = '0.0'
TOOL.ClientConVar['model'] = 'models/props_interiors/pot01a.mdl'

models = {
	{ 'Teapot', 'models/props_interiors/pot01a.mdl' },
	{ 'Pot', 'models/props_interiors/pot02a.mdl' },
	{ 'Skull', 'models/Gibs/HGIBS.mdl' },
	{ 'Clock', 'models/props_c17/clock01.mdl' },
	{ 'Hula Doll', 'models/props_lab/huladoll.mdl' }
}

if ( CLIENT ) then
	language.Add( 'Airbrake', 'Airbrake' )
	language.Add( 'Tool_airbrake_name', "Malawar's Airbrake Creator" )
	language.Add( 'Tool_airbrake_desc', 'Make Airbrakes. Good for generating resistance on airplanes or for stopping hovercraft/etc.' )
	language.Add( 'Tool_airbrake_0', 'Click where you wuold like to create an airbrake.' )

	language.Add( 'Undone_airbrake', 'Airbrake Undone' )
	language.Add( 'Cleanup_airbrake', 'Airbrake' )
	language.Add( 'Cleaned_airbrake', 'Cleaned up all Airbrakes' )
	language.Add( 'SBoxLimit_airbrakes', 'Maximum Airbrakes Reached' )
end


function TOOL:LeftClick( trace )
	if trace.Entity && (trace.Entity:IsPlayer() ) then return false end
	if(CLIENT) then
		return true
	end

	if(!SERVER) then return false end

	if ( !self:GetSWEP():CheckLimit( "airbrakes" ) ) then return false end

	local ply = self:GetOwner()
	local Pos = trace.HitPos
	local Ang = trace.HitNormal:Angle()
	Ang.pitch = Ang.pitch + 90
	
	local key				= self:GetClientNumber( 'key' ) 
	local passive			= self:GetClientNumber( 'passive' ) 
	local active			= self:GetClientNumber( 'active' ) 
	local model 			= self:GetClientInfo( 'model' ) 
	
	ent = MakeAirbrake( ply, Ang, Pos, key, passive, active, model)
	ent:SetPos( trace.HitPos - trace.HitNormal * ent:OBBMins().z)
	
	if ( trace.Entity:IsValid() ) then
		const = constraint.Weld(ent, trace.Entity,0, trace.PhysicsBone, 0, systemmanager )
		nocollide = constraint.NoCollide( ent, trace.Entity, 0, trace.PhysicsBone )
		trace.Entity:DeleteOnRemove( ent )
	end

	undo.Create('airbrake')
		undo.AddEntity( ent)
		undo.AddEntity( const )
		undo.AddEntity( nocollide )
		undo.SetPlayer( ply )
	undo.Finish()


	ply:AddCleanup( 'airbrakes', thruster )
	ply:AddCleanup( 'airbrakes', const )
	ply:AddCleanup( 'airbrakes', nocollide )
	
	return true
end


if SERVER then 
    CreateConVar('sbox_maxairbrakes',5)
	
	function MakeAirbrake( ply, Ang, Pos, key, passive, active, model)
		if ( !ply:CheckLimit( "airbrakes" ) ) then return nil end
		local ent = ents.Create( 'malawar_airbrake' )
			if !ent:IsValid() then return false end
			ent:SetModel( model )
			ent:SetAngles(Ang)
			ent:SetPos(Pos)
		ent:Spawn()

		ent:SetVar('Owner',ply)
		
		numpad.OnDown(ply, key, 'FireAirbrake', ent, 1)
		numpad.OnUp(ply, key, 'FireAirbrake', ent, 0)
		
		local rtable = {
			key = key,
			passive = passive,
			active = active,
			model = model
			}
			
		table.Merge(ent:GetTable(), rtable )
		
		ply:AddCount('airbrakes', ent)
		
		return ent
	end
	
	function FireAirbrake( ply, ent, toggle )
		if !ent:IsValid() then return false end
		ent:GetTable().braking = toggle
	end
	
	numpad.Register( "FireAirbrake", FireAirbrake )
end


function TOOL.BuildCPanel( cp )
	cp:AddControl( 'Header', { Text = '#Tool_airbrake_name', Description	= '#Tool_airbrake_desc' }  )

	combo = {}
	combo.Label = 'Airbrake Model'
	combo.MenuButton = 1
	combo.Folder = 'settings/menu/main/construct/airbrake/'
	combo.Options = {}
	
	for k, v in pairs(models) do
	  combo.Options[v[1]] = { airbrake_model = v[2] }
	end
	
	cp:AddControl('ComboBox', combo) 
	cp:AddControl(  "Label", { Text = "Passive braking is always on, while active braking is only\nin effect when you hold the button down." } )

	
	cp:AddControl( 'Slider', { Label = 'Passive Braking %', Type = "Float", Min = 0, Max = 100, Command = 'airbrake_passive' } )
	cp:AddControl( 'Slider', { Label = 'Active Braking %', Type = "Float", Min = 0, Max = 100, Command = 'airbrake_active' } )
	
	
	cp:AddControl('Numpad', { 
		ButtonSize = '22', 
		Label = 'Activate', 
		Command = 'airbrake_key',
		} )
end

function TOOL:UpdateSpawnGhost( ent, player )
	if ( !ent ) then return end
	if ( !ent:IsValid() ) then return end

	local tr 	= utilx.GetPlayerTrace( player, player:GetCursorAimVector() )
	local trace 	= util.TraceLine( tr )
	if (!trace.Hit) then return end
	
	if (trace.Entity && trace.Entity:GetClass() == "malawar_airbrake" || trace.Entity:IsPlayer() ) then
	
		ent:SetNoDraw( true )
		return
		
	end
	
	local Ang = trace.HitNormal:Angle()
	Ang.pitch = Ang.pitch + 90
	ent:SetAngles( Ang )
	
	local min = ent:OBBMins()
	ent:SetPos( trace.HitPos - trace.HitNormal * min.z )
	
	ent:SetNoDraw( false )
end


function TOOL:Think()
	local model	= self:GetClientInfo('model')
	
	if (!self.GhostEntity || !self.GhostEntity:IsValid() || self.GhostEntity:GetModel() != model) then
		self:MakeGhostEntity( model, Vector(0,0,0), Angle(0,0,0) )
	end
	
	self:UpdateSpawnGhost( self.GhostEntity, self:GetOwner() )
end
